package com.im.controller;
import com.im.pojo.Column;
import com.im.pojo.User;
import com.im.service.ColumnService;
import com.im.util.JsonResult;
import com.im.util.Key;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
public class CloumnCtrl{

    @Autowired
    private ColumnService columnService;

    @RequestMapping(value="/mgr/column2",method = RequestMethod.POST)
    public JsonResult addcolumn(@RequestBody Column column, HttpSession session){
       JsonResult jr=new JsonResult(0,"添加成功！");
        User user=(User)session.getAttribute(Key.SESSION_USER);

        int num=0;
        try {
            num=columnService.save(column,user);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        if(num>0){
            jr.setCode(0);
            jr.setMsg("添加成功");
        }else{
            jr.setCode(1);
            jr.setMsg("添加失败");
        }
        return jr;
    }

    @RequestMapping(value="/mgr/column2" ,method = RequestMethod.DELETE)
    public JsonResult deletecolumn(@RequestBody Column column, HttpSession session){
        JsonResult jr=new JsonResult(0,"删除成功！");
        User user=(User)session.getAttribute(Key.SESSION_USER);
        int num=0;
        try {
            num=columnService.delete(column,user);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        if(num>0){
            jr.setCode(0);
            jr.setMsg("删除成功");
        }else{
            jr.setCode(1);
            jr.setMsg("删除失败");
        }
        return jr;
    }
    @RequestMapping(value="/mgr/column2" ,method = RequestMethod.GET)
    public JsonResult showcolumnById(@RequestBody Column column){
        JsonResult jr=new JsonResult(0,"成功！");
        try {
            column=columnService.query(column);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        if(column!=null){
            jr.setData(column);
        }else{
            jr.setCode(1);
            jr.setMsg("查无此数据");
        }
        return jr;
    }
//
//    /*
//    修改 put请求
//    发送数据：column，user，id
//    调用model实现修改功能
//     */
    @RequestMapping(value="/mgr/column2" ,method = RequestMethod.PUT)
    public JsonResult updatecolumnById(@RequestBody Column column, HttpSession session){
        JsonResult jr=new JsonResult(0,"成功！");
        User user=(User)session.getAttribute(Key.SESSION_USER);
        int num=0;
        try {
            num=columnService.update(column,user);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        if(num>0){
            jr.setCode(0);
            jr.setMsg("修改成功");
        }else{
            jr.setCode(1);
            jr.setMsg("修改失败");
        }
        return jr;
    }
    @RequestMapping(value="/mgr/column2s",method = RequestMethod.POST)
    public JsonResult showcolumn(@RequestBody Column column, HttpSession session){
        JsonResult jr=new JsonResult(0,"成功！");
        User user=(User)session.getAttribute(Key.SESSION_USER);
        List list=null;
        try {
            list=columnService.queryAll(column,user);
            if(list!=null && list.size()>0){
                jr.setData(list);
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }

        return jr;
    }
}
